/*
 * @Author: c
 * @Date: 2024-01-09 12:24:59
 * @LastEditTime: 2024-03-28 07:58:38
 * @LastEditors: c
 * @Description: 
 * @FilePath: \music-web-backend\src\core\music\song\song.controller.ts
 * https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Body, Controller, Delete, Get, Param, ParseArrayPipe, ParseIntPipe, Post, Put, Query, Req } from '@nestjs/common';
import { Request } from 'express';
import { SkipAuth } from 'src/core/system/auth/constants';
import { FavoriteForm, SongForm } from './dto/create-song.dto';
import { SongPageQuery, TrackLinkQueryDto } from './dto/search-song.dto';
import { SongService } from './song.service';

@Controller('song')
export class SongController {
  constructor(private readonly songService: SongService) { }



  /**
    * 歌曲-播放列表
    * @param pageDto 
    * @returns 
    */
  @SkipAuth()
  @Get("playlist")
  getPlaylist(@Req() req, @Query() pageDto: SongPageQuery) {
    return this.songService.findAll(req.user, pageDto);
  }

  /**
    * 歌曲-分页查询
    * @param pageDto 
    * @returns 
    */
  @SkipAuth()
  @Get("page")
  getPage(@Req() req, @Query() pageDto: SongPageQuery) {
    return this.songService.findAll(req.user, pageDto);
  }

  /**
    * 歌曲-我收藏的歌曲
    * @param pageDto 
    * @returns 
    */
  @Get("favorite")
  getFavorite(@Req() req, @Query() pageDto: SongPageQuery) {
    return this.songService.myfavorite(req.user, pageDto);
  }

  /**
    * 歌曲-新增-我收藏的歌曲
    * @param songForm 
    * @returns 
    */
  @Post("favorite")
  createFavorite(@Req() req, @Body() songForm: FavoriteForm) {
    return this.songService.createfavorite(req.user, songForm);
  }

  /**
    * 歌曲-删除-我收藏的歌曲
    * @param songForm 
    * @returns 
    */
  @Delete("favorite")
  deletefavorite(@Req() req, @Body() songForm: FavoriteForm) {
    return this.songService.deletefavorite(req.user, songForm);
  }

  /**
   * 歌曲-下拉选项
   * @param songName 歌曲名称 
   * @returns 
   */
  @Get("options")
  async listSongOptions(@Query() { songName, artistIds }: SongPageQuery) {
    const songOptions = await this.songService.listSongOptions(songName, artistIds)
    return songOptions
  }

  /**
   * 歌曲-新增
   * @param pageDto 
   * @returns 
   */
  @Post()
  saveSong(@Body() songForm: SongForm) {

    return this.songService.saveSong(songForm); //必须return 不然报错终止整个运行的进程
  }


  /**
  * 歌曲-单条表单数据
  * @param typeCode 
  * @returns 
  */
  @SkipAuth()
  @Get(":id/form")
  getSongFormData(@Req() req: Request,
    @Param('id', ParseIntPipe) id
  ) {
    return this.songService.getSongFormData(req.user, id);
  }

  /**
  * 歌曲-播放视频
  * @param typeCode 
  * @returns 
  */
  @Get("tracklink")
  getTracklink(@Req() req: Request,
    @Query() trackLink: TrackLinkQueryDto
  ) {
    return this.songService.getTrackLink(req, trackLink);
  }


  /**
   * 歌曲-修改
   * @param songForm 
   * @returns 
   */
  @Put(':songId')
  async updateSong(@Param('songId', ParseIntPipe) songId: number, @Body() songForm: SongForm) {

    return await this.songService.update(songId, songForm); //必须return 不然报错终止整个运行的进程
  }

  /**
   * 歌曲-删除
   * @param id
   * @returns 
   */
  @Delete(':ids')
  remove(@Param('ids', ParseArrayPipe) ids: number[]) {
    return this.songService.removeByIds(ids);
  }

  /**
   * 歌曲-封面-删除
   * @param id 歌曲id
   * @param picId 封面id
   * @returns 
   */
  @Delete('deletepic/:id/:picId')
  deleteCoverPic(@Param('id', ParseIntPipe) id: number, @Param('picId') picId: string) {
    return this.songService.deleteCoverPic(id, picId);
  }

  /**
   * 歌曲-音频-删除
   * @param sondId 歌曲id
   * @param rateIds 音频id
   * @returns 
   */
  @Delete('delrates/:id/:rateIds')
  deleteRateFiles(@Param('id', ParseIntPipe) sondId: number, @Param('rateIds', ParseArrayPipe) rateIds: number[]) {

    return this.songService.deleteRateFiles(sondId, rateIds);
  }



}
